package com.damon;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.damon.entity.ApiEntity;
import com.damon.log.WcsLog;
import com.damon.thread.ApiThread;
import com.damon.utils.ByteUtil;
import com.damon.utils.DbUtil;
import com.damon.utils.GlobalOjbectUtil;
import com.damon.utils.LogUtil;
import com.damon.utils.LogbackLoadUtil;
import com.damon.utils.PropertiesUtil;
import com.damon.utils.ThreadPoolUtil;
import com.damon.window.WcsWindow;

public class WcsUploadImgRun {
//	static Logger logger = LoggerFactory.getLogger(WcsPrinterRun.class);
//	public static Map<String, Queue<String>> addressMap = new HashMap<>();
	public static void main(String[] args) {
		//加载外部logback配置文件
		String logbackPath = LogbackLoadUtil.loadExportXml();
//		String PropFilePath = PropertiesUtil.loadPropFile();
//		String DBFilePath = DbUtil.loadPropFile();
		//实例化可视化界面
		WcsWindow wcsWindow = new WcsWindow();
		GlobalOjbectUtil.setWindow(wcsWindow);
		//加载日志
		WcsLog logger = LogUtil.getLogger(WcsUploadImgRun.class);
		
		logger.info("日志加载路径："+logbackPath);
//		logger.info("通用配置文件加载路径："+PropFilePath);
//		logger.info("数据库文件加载路径："+DBFilePath);
		PropertiesUtil.getPropertiesMap();
		logger.info("正在检测需要调用的接口。。。。。。。。");
		String sql = "select id,config_type,config_desc,url,file_path,machine_code,site_code,blocking_time from wcs_http_img_config where is_used='1'";
		Connection connection = DbUtil.getConnection();
		Statement statement = null;
		ResultSet resultSet;
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while(resultSet.next()) {
				ApiEntity apiEntity = new ApiEntity();
				apiEntity.setId(resultSet.getInt("id"));
				apiEntity.setConfigType(resultSet.getString("config_type"));
				apiEntity.setConfigDesc(resultSet.getString("config_desc"));
				apiEntity.setUrl(resultSet.getString("url"));
				apiEntity.setFilePath(resultSet.getString("file_path"));
				apiEntity.setMachineCode(resultSet.getString("machine_code"));
				apiEntity.setSiteCode(resultSet.getString("site_code"));
				apiEntity.setBlockingTime(resultSet.getInt("blocking_time"));
				GlobalOjbectUtil.getApiList().add(apiEntity);
			}
		} catch (SQLException e) {
			logger.error("查询待调用接口失败！！");
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//为每个待调用的接口创建线程
		logger.info("已检测到："+GlobalOjbectUtil.getApiList().size()+" 个待回传的接口，开始创建线程！");
		for(ApiEntity apiInfo : GlobalOjbectUtil.getApiList()) {
//			Thread test = new Api
			ThreadPoolUtil.getExecutor().execute(new ApiThread(apiInfo));
		}
	}
	
}
